#ifndef _BUFFER_H_ 
#define _BUFFER_H_

#include <stdlib.h>


/***************************************************
 * user customize
 ***************************************************/

/* ---- memory allocating method ---- */
#define BUF_MALLOC(size)	malloc(size)
#define BUF_FREE(ptr)       free(ptr)


#define CONDITION_STR_MAX_LEN	100		/* no need to modify most of the time */
#define CDT_BUF_SIZE		81920		/* size of condition buffer */
#define LEN_BUF_SIZE		1024*512	/* size of fixed length buffer */

/***************************************************
 * internal definition. DO NOT MODIFY
 ***************************************************/

#define FOUND			1	/* use in conditon buffer. when 'conditon_str' is found, FOUND returned */
#define FULL			1	/* use in fix length buffer. when buffer really full, data needs to be copy to another space. */
#define STOP			2	/* use in fix length buffer. when buffer reach the stop size */
#define CONTINUE_BUF	0	/* use in conditon buffer and fix length buffer. when buffer needs continue for further data */
#define NO_DATA			-1	/* conditon buffer: no data comes. fix length buffer: no data comes and stop size is 0. */
#define BUF_IS_FULL 	-2	/* use in conditon buffer. when buffer is readlly full */
#define FOUND_AND_FULL	-3	/* use in conditon buffer. when 'conditon_str' is found and buffer is readlly full. some other data may be dropped. */


/*
 * condition buf struct.
 * this struct must be available all the running time
 */
typedef struct con_buf
{
	size_t buf_length;			/* buffer length specified by #define */
	char buf_ptr[CDT_BUF_SIZE + 1];	/* the buffer poninter */
	size_t buf_content_len; 	/* length of the content before conditon string */
	char condition_str[CONDITION_STR_MAX_LEN];		/* the buffer ending conditon */
	size_t conditon_str_len;	/* length of conditon string */
	char *remain_data;			/* the remain data after ending strings */
	size_t remain_data_len;		/* length of above data */
	size_t bytes_read;			/* ... function internal use */
}CONDITION_BUF;


/*
 * length buf struct.
 * this struct must be available all the running time
 */
typedef struct len_buf
{
	size_t buf_length;			/* buffer length specified by #define */
	size_t bytes_read;			/* ... function internal use */
	size_t write_ptr;
	size_t last_data_len;
	char buf_ptr[LEN_BUF_SIZE + 1];	/* the buffer poninter */
}LENGTH_BUF;




/* call the following functions to use buffer */

/*=================================================================
 * this part is API of condition buffer
 *=================================================================*/

/* defines for common users */
#define cdt_get_buf(buf_handler)			buf_handler->buf_ptr 			/* (char *) */
#define cdt_get_data_len(buf_handler)		buf_handler->buf_content_len 	/* (size_t) */
#define cdt_get_remain_data(buf_handler)	buf_handler->remain_data		/* (char *) */
#define cdt_get_remain_len(buf_handler)		buf_handler->remain_data_len	/* (size_t) */

/* buffer api */
CONDITION_BUF *condition_buf_init(const char *condition_str);
int condition_buf(CONDITION_BUF *buf_handler, char *data, size_t len);
int condition_buf_clean(CONDITION_BUF *buf_handler);
void condition_buf_reset(CONDITION_BUF *buf_handler);


/*=================================================================
 * this part is API of fixed length buffer
 *=================================================================*/

/* defines for common users */
#define len_get_buf(buf_handler)			buf_handler->buf_ptr 			/* (char *) */
#define len_get_data_len(buf_handler)		buf_handler->last_data_len 		/* (size_t) */

/* buffer api */
LENGTH_BUF *length_buf_init(void);
int length_buf(LENGTH_BUF *buf_handler, char *data, size_t len);
int length_buf_clean(LENGTH_BUF *buf_handler);
void length_buf_set_sl(LENGTH_BUF *buf_handler, size_t len);		/* set buffer stop length */
void length_buf_reset(LENGTH_BUF *buf_handler);

	
#endif


